var fs = require('fs-extra');
var path = require('path');
let sp = module.exports = {
    scan: function (pp, suffix, cb) {

        if (!fs.existsSync(pp)) return;
        let myFiles = fs.readdirSync(pp);
        for (let i in myFiles) {
            let name = myFiles[i];
            let newPp = path.join(pp, name);
            let stat = fs.lstatSync(newPp);

            if (stat.isDirectory()) {
                sp.scan(newPp, suffix, cb);
            } else {
                if (name.substr(- suffix.length) == suffix) cb(newPp);
            }
        }
    },

    findFiles: function (p, tag, isNext) {
        let files = [];
        let dir = fs.readdirSync(p);
        dir.forEach(one => {
            let f = path.join(p, one);

            let stat = fs.statSync(f);
            if (stat.isDirectory()) { // 继续循环
                if (isNext) {
                    let ff = this.findFiles(f, tag, isNext)
                    ff.forEach(a => { files.push(a) });
                }
                return;
            }
            if (tag.test(one)) files.push({ path: p, name: one });
        });
        return files;
    },

    findChildDirNames: function (p, tag) {
        let files = [];
        let dir = fs.readdirSync(p);
        dir.forEach(one => {
            let f = path.join(p, one);
            let stat = fs.statSync(f);
            if (stat.isDirectory()) {// 继续循环
                if (tag) {
                    if (tag.test(f)) files.push({ path: p, name: one });
                } else {
                    files.push({ path: p, name: one });
                }
                return;
            }
        });
        return files;

    },
};